package com.maigao.api.controller;

import com.maigao.api.interceptors.identify.Identify;
import com.maigao.entity.ResultJson;
import com.maigao.entity.enums.ResultStatus;
import com.maigao.entity.form.AddStudentCourseForm;
import com.maigao.entity.vo.StudentCourseVO;
import com.maigao.service.StudentCourseService;
import com.maigao.service.StudentService;
import com.maigao.util.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2024-10-21 22:57
 **/
@RestController
@RequestMapping(value = "/studentCourse")
public class StudentCourseController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private StudentCourseService studentCourseService;

    @Identify
    @ResponseBody
    @RequestMapping("listAll")
    public ResultJson<List<StudentCourseVO>> listAll() {
        List<StudentCourseVO> studentCourses = studentCourseService.tryFindAll();
        if (CollectionUtils.isEmpty(studentCourses)) {
            return new ResultJson<>(ResultStatus.NO_DATA);
        }
        return ResultJson.success(studentCourses);
    }

    @Identify
    @ResponseBody
    @RequestMapping("listAllByStudent")
    public ResultJson<List<StudentCourseVO>> listAllByStudent(@RequestParam Integer studentId) {
        List<StudentCourseVO> studentCourses = studentCourseService.tryFindAllByStudent(studentId);
        if (CollectionUtils.isEmpty(studentCourses)) {
            return new ResultJson<>(ResultStatus.NO_DATA);
        }
        return ResultJson.success(studentCourses);
    }

    @Identify
    @ResponseBody
    @RequestMapping("add")
    public ResultJson addStudentCourse(AddStudentCourseForm addStudentCourseForm) {
        Pair<Boolean, String> addResult = studentCourseService.tryAddStudentCourse(addStudentCourseForm);
        if (addResult.getLeft()) {
            return ResultJson.success("");
        }
        return ResultJson.fail(addResult.getRight());
    }

    @Identify
    @ResponseBody
    @RequestMapping("modifyStatus")
    public ResultJson modifyStatus(@RequestParam Integer id, @RequestParam Integer newStatus) {
        Pair<Boolean, String> modifyResult = studentCourseService.tryUpdateStatus(id, newStatus);
        if (modifyResult.getLeft()) {
            return ResultJson.success("");
        }
        return ResultJson.fail(modifyResult.getRight());
    }


}
